package njust.csc

import org.apache.poi.ss.usermodel.CellType
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.ss.usermodel.WorkbookFactory
import kotlin.io.path.Path
import kotlin.io.path.createDirectories
import kotlin.io.path.exists
import kotlin.io.path.moveTo

/**
 * @Author ZhouHang
 * @Date 24/8/7 周三
 * 处理 batch 数据的脚本
 */

val homePath = Path(System.getenv("APKEXPLORER_HOME"))
val datasetPath = homePath.resolve("input").resolve("batch")
val labelPath = homePath.resolve("input").resolve("batch_label")
val targetPath = homePath.resolve("input").resolve("dataset")

fun main() {
    classify("black")
}

fun classify(type: String) {
    val targetDirPath = targetPath.resolve(type)
    targetDirPath.createDirectories()
    val labelTable = labelPath.resolve("${type}.xlsx")
    val workbook = WorkbookFactory.create(labelTable.toFile())
    val workSheet = workbook.getSheetAt(0)
    workSheet.rowIterator().forEach { row: Row ->
        row.getCell(0).setCellType(CellType.STRING)
        val apkName = row.getCell(0).stringCellValue
        val sourceApkPath = datasetPath.resolve("$apkName.apk")
        if (sourceApkPath.exists()) {
            val targetApkPath = targetDirPath.resolve("$apkName.apk")
            sourceApkPath.moveTo(targetApkPath, true)
        } else {
            println("[$type] 文件不存在: $sourceApkPath")
        }
    }
}
